package com.hexun.dubbo.demo.service;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.hexun.dubbo.JsonMixins;
import com.hexun.dubbo.anno.ApiMonitor;
import com.hexun.dubbo.counter.RollingCounterPool;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * UserServiceImpl
 *
 * @author yuanyue
 * @date 2016/4/27
 */
@Service
@Path("user")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class UserServiceImpl implements UserService {
    @GET
    @Path("m1/{uid}")
    @JsonMixins(mixins = JsonMixinConfig.User12313123.class, targets = User.class)
    @ApiMonitor(value = "根据用户 id 调用用户信息", monitorData = "{\"uid\":123}", monitorRegex = "123123123123")
    @Override
    public User getUser(@PathParam("uid") Integer uid) {
        User user = new User();
        user.setPassword("123123123123");
        user.setUsername("袁跃265" + uid);

        LoggerFactory.getLogger(this.getClass()).error("这是侧是");

        return user;
    }


    @GET
    @Path("m2")
    @JsonMixins(mixins = JsonMixinConfig.User1.class, targets = User.class)
    @Override
    public User getUser1() {
        User user = new User();
        user.setPassword("123123123123");
        user.setUsername("袁跃265");
        return user;
    }

    @POST
    @Path("m3")
    @Override
    public User getUser2() {
        User user = new User();
        user.setPassword("123123123123");
        user.setUsername("袁跃265");
        return user;
    }

    @GET
    @Path("m4")
    @Override
    public String getUser3() {
        return "fsdfsf";
    }

    public static void main(String[] args) throws InterruptedException, IOException, ExecutionException {

        RollingCounterPool pool = new RollingCounterPool(100, 20000, 200);
        pool.record("123123");
        for (int j = 0; j < 100; j++) {
            pool.record("123123");

        }
        System.out.println(pool.gt("123123", 1000));
        System.out.println(pool.gt("123123", 10));
        pool.gt("123123", 10);


    }

}
